package com.mf.dispatcher;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.alibaba.fastjson.JSON;
import com.mf.controller.MusicController;
import com.mf.controller.PictureController;
import com.mf.daoImpl.MusicDaoImpl;
import com.mf.entity.Music;
import com.mf.entity.Picture;


@WebServlet("/picture/*")
public class PictureDispatcher extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private String traget;
	PictureController pictureController=new PictureController();
	MusicController musicController=new MusicController();
	MusicDaoImpl musicDaoImpl=new MusicDaoImpl();
	

    public PictureDispatcher() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String url= request.getRequestURI();
		String myUrl = url.substring(url.lastIndexOf("/")+1);
		response.setContentType("application/json; charset=utf-8"); 
		PrintWriter out=response.getWriter();
		String jsonString;
		String picId;
		String musicId;
		Picture picture=new Picture();
		Music music=new Music();
		
		switch(myUrl){
		case "allPicture":
		    jsonString=JSON.toJSONString(pictureController.list(request).add("musicList", musicDaoImpl.getAllMusic()));
			traget = jsonString;
			out=response.getWriter();
			out.println(traget);
			break;
		case "delPicture":
		    picId=request.getParameter("id");
			String realname=request.getParameter("realname");
			 jsonString=JSON.toJSONString(pictureController.deletePicture(picId,realname));
			traget = jsonString;
			out=response.getWriter();
			out.println(traget);
			break;
		case "delMusic":
		    musicId=request.getParameter("id");
			String pictitle=request.getParameter("pictitle");
			String musicrealname=request.getParameter("musicrealname");
			 jsonString=JSON.toJSONString(musicController.delMusic(musicId, pictitle,musicrealname));
			traget = jsonString;
			out=response.getWriter();
			out.println(traget);
			break;
		case "getPicture":
		   picId=request.getParameter("id");
			 jsonString=JSON.toJSONString(pictureController.getPictureById(picId));
			traget = jsonString;
			out=response.getWriter();
			out.println(traget);
			break;
		case "getMusic":
			 musicId=request.getParameter("id");
			 jsonString=JSON.toJSONString(musicController.getMusic(musicId));
			traget = jsonString;
			out=response.getWriter();
			out.println(traget);
			break;
		case "updatePicture":
			picture.setId(Integer.parseInt(request.getParameter("id")));
			picture.setBuilder(request.getParameter("builder"));
			picture.setTitle(request.getParameter("title"));
			picture.setIntro(request.getParameter("intro"));
			picture.setDetalis(request.getParameter("detalis"));
			picture.setIsmusicpic(0);
			picture.setRealname(request.getParameter("realname"));
			picture.setType(request.getParameter("type"));
			 jsonString=JSON.toJSONString(pictureController.pictureUpadte(picture));
			traget = jsonString;
			out=response.getWriter();
			out.println(traget);
			break;
		case "updateMusic":
			picture.setId(Integer.parseInt(request.getParameter("pictureid")));
			picture.setBuilder(request.getParameter("builder"));
			picture.setTitle(request.getParameter("title"));
			picture.setIntro(request.getParameter("intro"));
			picture.setDetalis(request.getParameter("detalis"));
			picture.setIsmusicpic(0);
			picture.setRealname(request.getParameter("picrealname"));
			picture.setType(request.getParameter("type"));
			
			music.setId(Integer.parseInt(request.getParameter("musicid")));
			music.setMusicname(request.getParameter("musicname"));
			music.setPictitle(request.getParameter("picrealname"));
			music.setRealname(request.getParameter("musicrealname"));
			 jsonString=JSON.toJSONString(musicController.musicUpate(music,picture));
			traget = jsonString;
			out=response.getWriter();
			out.println(traget);
		
			break;
		}
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	

    

}
